import config from "./config.js"
export const request = (options) => {
	return new Promise((resolve, reject) => {
		let token = uni.getStorageSync('token')
		// let token = '500da5ac8cf7fac349f702ec5a2de420'
		// 在登录的时候需要储存 token uni.setStorageSync("token","这里是登录获取的token值")
		uni.request({
			url: config.baseUrl + options.url,
			data: options.data || {},
			method: options.method || 'POST',
			header: options.header || {
				'access-token': token,
			},
			success: (res) => {
				if (res.data.status == 200) {
					resolve(res.data)
				}
				if (res.data.status == 401) {
					uni.showToast({
						title: '登录失效,请重新登录',
						icon: 'none'
					})
					setTimeout(() => {
						uni.setStorageSync("token", " ")
						uni.navigateTo({
							url: '/pages/login'
						})
					}, 1000)
				}
				if (res.data.status == 500) {
					uni.showToast({
						title: res.data.message,
						icon: 'none'
					})
					// 返回错误消息
					reject(res.data)
				}
			},
			fail: (err) => {
				// 页面中弹框显示失败
				uni.showToast({
					title: '请求接口失败',
					icon: 'none'
				})
				// 返回错误消息
				reject(err)
			},
			catch: (e) => {
				resolve(e)
			}
		})
	})
}
// 将对象导出外部引入使用
export default {
	request
}
